from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse

class ToolCategory(models.Model):
    """工具分类模型"""
    name = models.CharField(max_length=100, verbose_name='分类名称')
    slug = models.SlugField(max_length=100, unique=True, verbose_name='URL别名')
    description = models.TextField(blank=True, verbose_name='分类描述')
    icon = models.CharField(max_length=50, default='fas fa-tools', verbose_name='图标类名')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '工具分类'
        verbose_name_plural = '工具分类'
        ordering = ['name']
    
    def __str__(self):
        return self.name
    
    def get_absolute_url(self):
        return reverse('tools:category_detail', kwargs={'slug': self.slug})

class Tool(models.Model):
    """工具模型"""
    STATUS_CHOICES = [
        ('active', '正常'),
        ('inactive', '停用'),
    ]
    
    TYPE_CHOICES = [
        ('online', '在线工具'),
        ('software', '软件工具'),
        ('library', '开发库'),
        ('service', '在线服务'),
        ('resource', '资源站点'),
    ]
    
    name = models.CharField(max_length=200, verbose_name='工具名称')
    slug = models.SlugField(max_length=200, unique=True, verbose_name='URL别名')
    category = models.ForeignKey(ToolCategory, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='分类')
    description = models.TextField(verbose_name='工具描述')
    url = models.URLField(verbose_name='工具链接')
    icon = models.CharField(max_length=50, default='fas fa-link', verbose_name='图标类名')
    screenshot = models.ImageField(upload_to='tools/', blank=True, null=True, verbose_name='截图')
    tool_type = models.CharField(max_length=20, choices=TYPE_CHOICES, default='online', verbose_name='工具类型')
    is_featured = models.BooleanField(default=False, verbose_name='是否推荐')
    is_free = models.BooleanField(default=True, verbose_name='是否免费')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='active', verbose_name='状态')
    views = models.PositiveIntegerField(default=0, verbose_name='浏览次数')
    clicks = models.PositiveIntegerField(default=0, verbose_name='点击次数')
    rating = models.DecimalField(max_digits=3, decimal_places=2, default=0.00, verbose_name='评分')
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='添加者')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '工具'
        verbose_name_plural = '工具'
        ordering = ['-created_at']
    
    def __str__(self):
        return self.name
    
    def get_absolute_url(self):
        return reverse('tools:tool_detail', kwargs={'slug': self.slug})
    
    def increment_views(self):
        """增加浏览次数"""
        self.views += 1
        self.save(update_fields=['views'])
    
    def increment_clicks(self):
        """增加点击次数"""
        self.clicks += 1
        self.save(update_fields=['clicks'])

class ToolReview(models.Model):
    """工具评价模型"""
    tool = models.ForeignKey(Tool, on_delete=models.CASCADE, related_name='reviews', verbose_name='工具')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    rating = models.IntegerField(choices=[(i, i) for i in range(1, 6)], verbose_name='评分')
    comment = models.TextField(blank=True, verbose_name='评价内容')
    is_approved = models.BooleanField(default=True, verbose_name='是否审核通过')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '工具评价'
        verbose_name_plural = '工具评价'
        ordering = ['-created_at']
        unique_together = ['tool', 'user']  # 每个用户只能对同一工具评价一次
    
    def __str__(self):
        return f'{self.user.username} 对 "{self.tool.name}" 的评价'

class ToolTag(models.Model):
    """工具标签模型"""
    name = models.CharField(max_length=50, unique=True, verbose_name='标签名称')
    slug = models.SlugField(max_length=50, unique=True, verbose_name='URL别名')
    tools = models.ManyToManyField(Tool, blank=True, verbose_name='关联工具')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '工具标签'
        verbose_name_plural = '工具标签'
        ordering = ['name']
    
    def __str__(self):
        return self.name
